﻿
using DLL.NET60.DB.MS.Entity.DTO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace DLL.NET50.DB.MS.AdviceAndDrug
{
    public class CUsedDrugsFile
    {
        public static CUsedDrugs usedDrugs = new CUsedDrugs();
        
        public static bool Read()
        {
            XmlSerializer xmlFormat = new XmlSerializer(typeof(CUsedDrugs));
            //string str2 = Application.StartupPath;
            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
            
            string filePath = str_1 + @"SavedDrugs\CUsedDrugsFile.xml";
            if (File.Exists(filePath))
            {
                Stream fstream = File.OpenRead(filePath);
                usedDrugs = (CUsedDrugs)xmlFormat.Deserialize(fstream);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Read(string path)
        {
            XmlSerializer xmlFormat = new XmlSerializer(typeof(CUsedDrugs));
            //string str2 = Application.StartupPath;
            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;

            string filePath = path; //str_1 + @"SavedDrugs\CUsedDrugsFile.xml";
            if (File.Exists(filePath))
            {
                Stream fstream = File.OpenRead(filePath);
                usedDrugs = (CUsedDrugs)xmlFormat.Deserialize(fstream);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Save()
        {
            XmlSerializer xmlFormat = new XmlSerializer(typeof(CUsedDrugs));
            //现在将数据保存到本地的一个文件中
            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
            string path = str_1 + @"SavedDrugs";
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            string filePath = str_1 + @"SavedDrugs\CUsedDrugsFile.xml";
            Stream fstream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
            xmlFormat.Serialize(fstream, usedDrugs);
            fstream.Close();
            return true;
        }
    }
    [Serializable]
    public class CUsedDrugs
    {
        public List<DTO_adi_dic_drug> lLocalBackupDrugs = new List<DTO_adi_dic_drug>();
    }
}
